8C - Looking for Order - CodeForces Solution


bitmasks dp *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
//#define int long long
//vector<vector<vector<int> > >dp(n+1,vector<vector<int> >(63,vector<int>(8,0)));
const long long inf=1e12;
long long dp[1<<24|1];
long long pre[1<<24|1];
struct node
{
    int x;
    int y;
}a[30];
long long dis[26][26];
void solve()
{
    cin>>a[0].x>>a[0].y;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
    }
    for(int i=0;i<=(1<<n);i++){
        dp[i]=inf;
    }
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            dis[i][j]=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
        }
    }
    pre[0]=0;
    dp[0]=0;
    for(int i=0;i<(1<<n);i++){
        if(dp[i]==inf){
            continue;
        }
        for(int j=1;j<=n;j++){
            if((i>>(j-1))&1){
                continue;
            }
            for(int k=1;k<=n;k++){
                if((i>>(k-1))&1){
                    continue;
                }
                if(dp[i]+dis[0][j]+dis[j][k]+dis[k][0]<dp[i|(1<<(j-1))|(1<<(k-1))]){
                    dp[i|(1<<(j-1))|(1<<(k-1))]=min(dp[i|(1<<(j-1))|(1<<(k-1))],dp[i]+dis[0][j]+dis[j][k]+dis[k][0]);
                    pre[i|(1<<(j-1))|(1<<(k-1))]=i;
                }
            }
            break;
        }
    }
    cout<<dp[(1<<n)-1]<<endl;
    long long cur=(1<<n)-1;
    while(cur){
        cout<<0<<' ';
        long long x=cur^pre[cur];
        for(int i=1;i<=n;i++){
            if((x>>(i-1))&1){
                cout<<i<<' ';
            }
        }
        cur=pre[cur];
    }
    cout<<0<<endl;
    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    T=1;
    //cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence